package com.ysxsoft.wdg.adapter.center_zy;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.library.flowlayout.FlowLayoutManager;
import com.library.flowlayout.SpaceItemDecoration;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.bean.zy.RuleBean;

import java.util.List;

public class GoodsRulesAdapter extends RecyclerView.Adapter<GoodsRulesAdapter.MyViewHolder> {

    private Context context;
    private List<RuleBean> data;

    private boolean isOneGroup = false;

    private OnChildClickListener onChildClickListener;

    public void setNewData(List<RuleBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public GoodsRulesAdapter(Context context, List<RuleBean> data) {
        this.context = context;
        this.data = data;

        if (getItemCount() <= 1) {
            isOneGroup = true;
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_goods_rules, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int gPosition) {
        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        holder.recyclerView.addItemDecoration(new SpaceItemDecoration(dp2px(6)));
        holder.recyclerView.setLayoutManager(flowLayoutManager);

        RulesAdapter rulesAdapter = new RulesAdapter(gPosition);
        holder.recyclerView.setAdapter(rulesAdapter);
        holder.tvTitle.setText(data.get(gPosition).getTitle());


    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public List<RuleBean> getData(){
        return data;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private RecyclerView recyclerView;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            recyclerView = itemView.findViewById(R.id.recyclerView);
        }
    }

    class RulesAdapter extends RecyclerView.Adapter<RulesAdapter.RulesHolder> {

        private List<RuleBean.SubBean> subBeanList;
        private int gPosition;

        public RulesAdapter(int gPosition) {
            this.gPosition = gPosition;
            subBeanList = data.get(gPosition).getSub();
            if (subBeanList.size() > 0) {
                subBeanList.get(0).setSelected(true);
            }
        }


        public void setOnlySelect(int position) {
            for (int i = 0; i < subBeanList.size(); i++) {
                subBeanList.get(i).setSelected(i == position);
            }
            notifyDataSetChanged();
        }


        @NonNull
        @Override
        public RulesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RulesHolder(View.inflate(context, R.layout.item_text2, null));
        }

        @Override
        public void onBindViewHolder(@NonNull RulesHolder holder, final int cPosition) {
            RuleBean.SubBean subBean = subBeanList.get(cPosition);
            holder.textView.setText(subBean.getTitle());
            holder.textView.setSelected(subBean.isSelected());

            holder.textView.setFocusable(true);

            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setOnlySelect(cPosition);
                    onChildClickListener.onChildClick(gPosition, cPosition);
                }
            });

        }

        @Override
        public int getItemCount() {
            return subBeanList == null ? 0 : subBeanList.size();
        }

        class RulesHolder extends RecyclerView.ViewHolder {

            private TextView textView;

            public RulesHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_goods_rules);
            }
        }


    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public interface OnChildClickListener {
        void onChildClick(int groupPosition, int childPosition);
    }

    private int dp2px(float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }
}
